package com.example.xinhuazidian.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.example.xinhuazidian.*
import com.example.xinhuazidian.bean.More

class MoreAdapter(val content: Context, private val list: MutableList<More>) :
    RecyclerView.Adapter<MoreAdapter.MyViewHolder>() {

    inner class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val ll: LinearLayout = view.findViewById(R.id.ll)
        val tvName: TextView = view.findViewById(R.id.tvName)
        val iv: ImageView = view.findViewById(R.id.iv)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_more, parent, false)
        return MyViewHolder(view)
    }

    private var mOnItemClickListener: OnRecyclerItemClickListener? = null

    fun setRecyclerItemClickListener(listener: OnRecyclerItemClickListener?) {
        mOnItemClickListener = listener
    }

    interface OnRecyclerItemClickListener {
        fun onRecyclerItemClick(more: More)
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        val appInfo = this.list[position]
        with(holder) {
            tvName.text = appInfo.name

            Glide.with(content).load(R.drawable.bg_shuimo).into(iv)

            ll.setOnClickListener {
                mOnItemClickListener?.onRecyclerItemClick(appInfo)
            }
        }
    }

    override fun getItemCount() = list.size
}

